package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.BookingPolicy;

public class BookingPolicyLogic extends AbstractBL {
	public List<BookingPolicy> GenerateBookingPolicy(long hotelId) {
		List<BookingPolicy> list = new ArrayList<BookingPolicy>();

		BookingPolicy bookingpolicy = new BookingPolicy();
		bookingpolicy.setHotelId(hotelId);
		bookingpolicy.setTitle("2 Day Booking");
		bookingpolicy.setDay(true);
		bookingpolicy.setChargeNight(false);
		bookingpolicy.setFix(false);
		bookingpolicy.setPercentcharge(true);
		bookingpolicy.setType("Credit");
		bookingpolicy.setCharge(50);
		bookingpolicy.setBeforeArrival(2);
		list.add(bookingpolicy);

		return list;
	}

	public BookingPolicy GetBookingPolicy(long id) {
		Key key = KeyFactory.createKey(EntityName.BookingPolicy, id);
		try {
			Entity lang = datastore.get(key);
			BookingPolicy c = new BookingPolicy();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}

	public List<BookingPolicy> GetAllBookingPolicy(long hotelId) {
		Query q = new Query(EntityName.BookingPolicy);

		Filter filterHotel = new FilterPredicate("hotelId",
				FilterOperator.EQUAL, hotelId);
		Filter filterAvaliable = new FilterPredicate("avalible",FilterOperator.EQUAL,true);
		
		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);

		return GetList(datastore.prepare(q).asList(
				FetchOptions.Builder.withDefaults()));
	}

	public BookingPolicy GetBookingPolicy(Entity entity) {
		BookingPolicy item = new BookingPolicy();
		item.setEntity(entity);
		return item;
	}

	public List<BookingPolicy> GetList(List<Entity> entities) {
		List<BookingPolicy> list = new ArrayList<BookingPolicy>();
		if(entities!=null)
		for (Entity e : entities) {
			BookingPolicy a = new BookingPolicy();
			a.setEntity(e);
			list.add(a);
		}
		return list;
	}

	public BookingPolicy AddBookingPolicy(long hotelId, String title,
			boolean isDay, boolean percentcharge, boolean fix, String type,
			long beforeArrival, double charge, boolean chargeNight)
			throws EntityNotFoundException {
		BookingPolicy c = new BookingPolicy();
		c.setHotelId(hotelId);
		c.setTitle(title);
		c.setPercentcharge(percentcharge);
		c.setFix(fix);
		c.setChargeNight(chargeNight);
		c.setBeforeArrival(beforeArrival);
		c.setCharge(charge);
		c.setDay(isDay);
		c.setType(type);
		c.setAvalible(true);
		c.setStatus(true);

		Key k = datastore.put(c.getEntity());
		c.setId(k.getId());
		c.setKey(k);

		return c;
	}

	public BookingPolicy EditBookingPolicy(long bookingpolicyId, String title,
			boolean isDay, boolean percentcharge, boolean fix, String type,
			long beforeArrival, double charge, boolean chargeNight)
			throws EntityNotFoundException {

		BookingPolicy c = GetBookingPolicy(bookingpolicyId);
		c.setTitle(title);
		c.setPercentcharge(percentcharge);
		c.setFix(fix);
		c.setChargeNight(chargeNight);
		c.setBeforeArrival(beforeArrival);
		c.setCharge(charge);
		c.setDay(isDay);
		c.setType(type);

		Key k = datastore.put(c.getEntity());
		c.setId(k.getId());
		c.setKey(k);
		return c;
	}

	public BookingPolicy DeleteBookingPolicy(long bookingpolicyId)
			throws EntityNotFoundException {
		BookingPolicy c = GetBookingPolicy(bookingpolicyId);
		c.setAvalible(false);
		c.setStatus(false);

		Key k = datastore.put(c.getEntity());
		c.setKey(k);
		c.setId(k.getId());
		return c;
	}

	public BookingPolicy ChangeStatus(long bookingpolicyId) throws EntityNotFoundException{
		
		BookingPolicy a = GetBookingPolicy(bookingpolicyId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public List<BookingPolicy> GetAllBookingPolicy4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.BookingPolicy);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avalible",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
}
